package com.lonely.fx.combo_box;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @date: 2022-07-18 10:05
 * @author: ztkj-hzb
 * @description: 下拉列表，用法跟 ChiceBox一样, 基于泛型是Object类型
 **/
public class ComboBoxWithObjectSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }


    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        //TODO 创建实例，用法跟 ChoiceBox一样
        ComboBox<User> comboBox = new ComboBox<>();

        User user1 = new User().setName("小A").setAge(10);
        User user2 = new User().setName("小B").setAge(20);
        User user3 = new User().setName("小C").setAge(30);
        User user4 = new User().setName("小D").setAge(40);
        User user5 = new User().setName("小E").setAge(50);
        comboBox.getItems().addAll(user1,user2,user3,user4,user5);

        //开启文本框
        comboBox.setEditable(true);

        //设置值转化逻辑
        comboBox.setConverter(new StringConverter<User>() {

            /**
             * 选取指定下拉值后进入这里/或者没有设置cellfactory的时候默认也会调用这里
             * @param object
             * @return
             */
            @Override
            public String toString(User object) {
                if(object == null){
                    return "";
                }

                String value = String.format("%s-%s", object.getName(), object.getAge());
                System.out.println(value);

                if(!comboBox.getItems().contains(object)){
                    comboBox.getItems().add(object);
                }

                return value;
            }

            /**
             * 开启文本框后，输入内容，回车，会进入到这里
             * @param string
             * @return
             */
            @Override
            public User fromString(String string) {
                //System.out.println(string);
                //return new User().setName(string).setAge(10);
                return new User().setName(string).setAge(10);
            }
        });


        /*comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                //回车,则添加成员
                if(event.getCode().equals(KeyCode.ENTER)){
                    comboBox.getItems().add(comboBox.getValue());
                    comboBox.show();
                }

            }
        });*/


        hBox.getChildren().addAll(comboBox);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.show();
        primaryStage.setHeight(500);

    }


    /**
     * 用户
     */
    private class User {

        private String name;

        private Integer age;

        public String getName() {
            return name;
        }

        public User setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public User setAge(Integer age) {
            this.age = age;
            return this;
        }
    }
}
